package cn.study.cas;

import java.util.concurrent.atomic.AtomicStampedReference;

/**
 * @Author     ：跟大佳一起学架构
 * @Description：演示带版本戳的原子操作类
 */
public class UseAtomicStampedReference {
    static AtomicStampedReference<String> asr = new AtomicStampedReference("老于",0);
    public static void main(String[] args) throws InterruptedException {
        //拿到当前的版本号(旧)
        final int oldStamp = asr.getStamp();
        final String oldReference = asr.getReference();
        System.out.println(oldReference+"============"+oldStamp);
        Thread rightStampThread = new Thread(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                System.out.println(name+":当前变量值："+oldReference + "-当前版本戳：" + oldStamp + "-更新结果："
                  + asr.compareAndSet(oldReference, oldReference + 1, oldStamp,oldStamp + 1));
            }
        });
        Thread errorStampThread = new Thread(new Runnable() {
            @Override
            public void run() {
                String reference = asr.getReference();
                System.out.println(Thread.currentThread().getName()  +":当前变量值：" +reference + "-当前版本戳：" + asr.getStamp() + "-更新结果："
                        + asr.compareAndSet(reference,  reference + 2, oldStamp, oldStamp + 1));
            }
        });
        rightStampThread.start();rightStampThread.join();
        errorStampThread.start();errorStampThread.join();
        System.out.println(asr.getReference()+"============"+asr.getStamp());
    }
}

